#ifndef _SOCKET_H_
#define _SOCKET_H_

#ifdef _WIN32
#include <winsock2.h>
struct sockaddr;
#else
typedef int SOCKET;
#include <sys/socket.h>
#endif
#include <string>
#include <vector>
#include "ssl.h"

class Socket
{
public:
  Socket();
  ~Socket();

  void EnableSSL();
  void Connect(const std::string& address, int port);
  bool Listen(int port);
  void AcceptConnection();
  bool IsConnected() const { return connected; }
  std::string ReadString();
  std::string ReadString(int timeOutMillisec);
  void WriteString(const std::string& s);
  void WriteBinary(const std::string& file, int range1, int range2);
  void WriteEnd();
  void Close();
  void CloseConnection();

  static int CheckConnections(const std::vector<Socket*>& sockets, int timeOutMillisec);

protected:
  Socket(const Socket& rhs);
  Socket& operator = (const Socket& rhs);

  SOCKET listener;
  SOCKET connection;
  sockaddr connectionAddr;
  bool connected;
  SSL* ssl;
};

#endif//_SOCKET_H_
